//
//  NotificationService.m
//  Molian
//
//  Created by wansong on 2/14/15.
//  Copyright (c) 2015 xiaocan. All rights reserved.
//

#import "NotificationService.h"

@implementation NotificationService

+ (void)handleRemoteNotification:(NSDictionary *)notificationPayload {
    // 处理需要全局响应的消息
    MLUser *currentUser = [MLUser currentUser];
    NSString *action = [notificationPayload objectForKey:@"action"];
    NSLog(@"action==%@",action);
    if(action){
        //
        if ([action isEqualToString:NOTIFICATION_TYPE_NEW_SYSYTEM_MASS_PUSH]) {
            //用通知发送给用户
            NSString *msg = [notificationPayload objectForKey:@"msg"];
            NSDictionary *paramsF = @{@"touserId": currentUser.objectId,@"msg":msg};
            [AVCloud callFunctionInBackground:@"sendSysMsg" withParameters:paramsF block:^(id object, NSError *error) {
                if (error) {
                    NSLog(@"发送错误");
                }
            }];
        }else if ([action isEqualToString:NOTIFICATION_TYPE_NEW_RECOMMEND_PUSH]){
            //编辑推荐红点
            [[NSNotificationCenter defaultCenter] postNotificationName:@"recommendPush" object:nil userInfo:nil];
            
        }else if ([action isEqualToString:NOTIFICATION_TYPE_NEW_CUSTOMAZITION_PUSH]) {
//#warning 新的情感热线,
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TYPE_NEW_CUSTOMAZITION_PUSH object:nil];
        }
    }
    
//    
//    if ([action isEqualToString:NOTIFICATION_TYPE_AUTHENTICATE_SUCCEED]) {
//        // 认证成功,刷新用户
//        [currentUser refreshInBackgroundWithBlock:nil];
//    } else if ([action isEqualToString:NOTIFICATION_TYPE_WITHDRAW_SUCCEED]) {
//        // 提现成功,刷新用户
//        [currentUser refreshInBackgroundWithBlock:nil];
//    } else if ([action isEqualToString:NOTIFICATION_TYPE_GET_SERVICE_INCOME]) {
//        // 获得了收入,刷新用户
//        [currentUser refreshInBackgroundWithBlock:nil];
//    } else if ([action isEqualToString:NOTIFICATION_TYPE_CHAT_BACKGROUND_UPDATED]) {
//        // 收到修改了背景的通知
//        [self updateChatBackground:notificationPayload];
//        // 让聊天页面也处理这个通知, 如果是当前聊天的对象, 则替换背景图
//        [[NSNotificationCenter defaultCenter]postNotificationName:action object:nil userInfo:notificationPayload];
//    } else {
//        // 如果这里不能处理, 转换为本地通知
//        [[NSNotificationCenter defaultCenter]postNotificationName:action object:nil userInfo:notificationPayload];
//    }
    
}

+ (void)handleOrderStart:(NSDictionary*)userInfo {
    // 将对方移出黑名单,同时用户选中的时候也需要将对方移出黑名单
    NSString *userId = userInfo[@"userId"];
  
}

+ (void)handleFinishedOrder:(NSDictionary*)userInfo {
    // 异步处理服务时间结束之后,将对方加入到黑名单中,禁止继续聊天
    // 等待1s再加黑名单,让它先收到提示消息
    [NSThread sleepForTimeInterval:1];
    // 将联系人加入黑名单
    NSString *userId = userInfo[@"userId"];
  
}

+ (void)updateChatBackground:(NSDictionary *)notiData{
    // 本地化指定聊天对象的背景图
    NSString *userId = notiData[@"userId"];
    AVQuery *userQuery = [MLUser query];
    MLUser *chatUser = (MLUser *)[userQuery getObjectWithId:userId];
    MLUser *currentUser = [MLUser currentUser];
    
    AVQuery *query = [MLChatBackground query];
    [query whereKey:@"status" equalTo:[NSNumber numberWithInt:MLChatBackgroundStatus_NORMAL]];
    NSArray *userArray = [NSArray arrayWithObjects:currentUser, chatUser, nil];
    [query whereKey:@"userA" containedIn:userArray];
    [query whereKey:@"userB" containedIn:userArray];
    [query addDescendingOrder:@"createdAt"];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        if (!error) {
            MLChatBackground *chatBackground = (MLChatBackground *)object;
            NSData *imgData = [chatBackground.img getData];
            UIImage *bgImg = [UIImage imageWithData:imgData];
            // 保存图片
            [Utils saveArchiveData:bgImg DIR:CHAT_BACKGROUND_DIR forKey:[NSString stringWithFormat:@"chatBackgroundImg_%@", userId]];
        }
    }];
}

@end
